package controller;

import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import model.DTO.DishPageDTO;
import model.VO.DishPageVO;
import model.VO.DishVO;
import service.DishService;
import service.Impl.DishServiceImpl;
import util.GetJsonUtil;
import util.ResultUtil;

import java.io.IOException;
import java.util.List;

@WebServlet("/admin/dish/page")
public class DishPageController extends HttpServlet {
    //分页查询菜品

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int page= Integer.parseInt(req.getParameter("page"));
        int pageSize= Integer.parseInt(req.getParameter("pageSize"));
        String name=req.getParameter("name");
        String categoryIdStr=req.getParameter("categoryId");
        Integer categoryId=null;
        if (categoryIdStr != null && !categoryIdStr.isEmpty()) {
                categoryId = Integer.parseInt(categoryIdStr);
        }
        String statusStr=req.getParameter("status");
        Integer status=null;
        if (statusStr != null && !statusStr.isEmpty()) {
            status = Integer.parseInt(statusStr);
        }
        DishPageDTO dishPageDTO=new DishPageDTO();
        dishPageDTO.setPage(page);
        dishPageDTO.setPageSize(pageSize);
        dishPageDTO.setCategoryId(categoryId);
        dishPageDTO.setName(name);
        dishPageDTO.setStatus(status);

        DishService dishService=new DishServiceImpl();

        DishPageVO dishPageVO=dishService.pageQuery(dishPageDTO);

        String json=ResultUtil.getJSON(dishPageVO);
        resp.getWriter().println(json);
//        DishPageDTO dpD= (DishPageDTO) GetJsonUtil.get(req,new DishPageDTO());
//        DishServiceImpl ds=new DishServiceImpl();
//        DishPageVO dpV=ds.pageQuery(dpD);
//        String json=ResultUtil.getJSON(dpV);
//        resp.getWriter().println(json);
    }
}
